#include "stdafx.h"
#include "ui_gdi_font_manager.h"

_SSL_BEGIN
_SSUIL_BEGIN

#define kDefaultFontId _T("__default__font_id___")
UIGdiFontManager::UIGdiFontManager()
{
	AddFont(kDefaultFontId, NULL,  -1, false, false, false);
	SetDefaultFont(kDefaultFontId);
}

UIGdiFontManager::~UIGdiFontManager()
{
	for(HFONTS::iterator iter=fonts_.begin(); iter!=fonts_.end(); iter++)
	{
		HFONT font = iter->second;
		if(font == NULL)
			continue;
		::DeleteObject(font);
		iter->second = NULL;
	}
	fonts_.clear();
	for(HANDLES::iterator iter=private_font_handles_.begin(); iter!=private_font_handles_.end(); iter++)
	{
		HANDLE h = *iter;
		if(h == NULL)
			continue;
		::RemoveFontMemResourceEx(h);
		*iter = NULL;
	}
	private_font_handles_.clear();
}

const tchar* UIGdiFontManager::GetFontType()
{
	return _T("HFONT");
}

bool UIGdiFontManager::ApplyPrivateFont(unsigned char *font_buffer, unsigned long font_buffer_size)
{
	if(font_buffer == NULL || font_buffer_size == 0)
		return false;

	DWORD font_number = 0;
	HANDLE h = ::AddFontMemResourceEx(font_buffer, font_buffer_size, NULL, &font_number);
	if(h == NULL)
		return false;
	if(font_number == 0)
	{
		::RemoveFontMemResourceEx(h);
		return false;
	}
	private_font_handles_.push_back(h);
	return true;
}

bool UIGdiFontManager::AddFont(const tchar *font_id, const tchar* font_name, int size,
	bool bold, bool underline, bool italic)
{
	HFONT font = CreateFontByParameters(font_name, size, bold, underline, italic);
	if(font == NULL)
		return false;

	HFONTS::iterator iter = fonts_.find(font_id);
	if(iter != fonts_.end())
	{
		if(iter->second != NULL)
		{
			::DeleteObject(iter->second);
			iter->second = NULL;
		}
	}
	fonts_[font_id] = font;
	return true;
}

UIFontD UIGdiFontManager::GetFont(const tchar *font_id)
{
	HFONTS::iterator iter = fonts_.find(font_id);
	if(iter == fonts_.end())
		iter = fonts_.find(default_font_id_);
	if(iter != fonts_.end())
	{
		return UIFontD(GetFontType(), (void*)iter->second);
	}
	return UIFontD();
}

bool UIGdiFontManager::IsFontExists(const tchar *font_id)
{
	return fonts_.find(font_id) != fonts_.end();
}

HFONT UIGdiFontManager::GetRawFont(const tchar *font_id)
{
	UIFontD font = GetFont(font_id);
	SS_ASSERT(font.font_type == GetFontType());
	return (HFONT)font.font;
}

bool UIGdiFontManager::SetDefaultFont(const tchar* font_id)
{
	if(font_id == NULL || _tcslen(font_id) == NULL)
		font_id = kDefaultFontId;
	default_font_id_ = font_id;
	return true;
}

LOGFONT UIGdiFontManager::GetSystemDefaultLogfont()
{
	LOGFONT lf = {0};
	if(::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
		return lf;
	::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(lf), &lf);
	return lf;
}

HFONT UIGdiFontManager::CreateFontByParameters(const tchar* font_name, int size, 
	bool bold, bool underline, bool italic)
{
	LOGFONT lf = GetSystemDefaultLogfont();
	lf.lfCharSet = DEFAULT_CHARSET;
	if(font_name != NULL && _tcslen(font_name) > 0)
		_tcscpy_s(lf.lfFaceName, font_name);
	if(size != -1)
		lf.lfHeight = -size;
	if(bold)
		lf.lfWeight += FW_BOLD;
	if(underline)
		lf.lfUnderline = 1;
	else
		lf.lfUnderline = 0;
	if(italic)
		lf.lfItalic = 1;
	else
		lf.lfItalic = 0;
	return ::CreateFontIndirect(&lf);
}

_SSUIL_END
_SSL_END
